﻿using DotNet.Utilities;
using PropertyChanged;
using System.IO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using DotNet.Utilities.ConsoleHelper;
using Microsoft.Extensions.DependencyInjection;
using WPFTemplateLib.WpfHelpers;
using WPFPractice.Services;

namespace WPFPractice.Windows
{
	/// <summary>
	/// WinDITester.xaml 的交互逻辑
	/// </summary>
	public partial class WinDITester : Window
    {
        public WinDITester(ITextService textService)
        {
            InitializeComponent();
            DataContext = new WinDITesterViewModel(textService);
        }

        #region Overrides of Window

        //protected override void OnClosing(CancelEventArgs e)
        //{
        //    base.OnClosing(e);
        //    e.Cancel = true;
        //    Hide();
        //}

        #endregion
    }

    [AddINotifyPropertyChangedInterface]
    public class WinDITesterViewModel : BindableBase
    {
        public WinDITesterViewModel(ITextService textService)
        {
            Console.SetOut(new ConsoleWriter(s =>
            {
                ShowInfoUc(s);
            }));

            //方式一：通过构造函数注入；
            _textService = textService;

            SetCommandMethod();
            InitValue();
        }

        #region 成员

        /// <summary>
        /// 通过构造函数注入的文字服务
        /// </summary>
        private ITextService _textService;

        #endregion

        #region 绑定

        /// <summary>
        /// 自动获取的文字（通过容器注入的文字服务获取）
        /// </summary>
        public string AutoGetText { get; set; }

        /// <summary>
        /// 手动获取的文字（通过主动从容器获取的文字服务获取）
        /// </summary>
        public string ManualGetText { get; set; }

        #endregion

        /// <summary>
        /// 数据初始化
        /// </summary>
        private void InitValue()
        {
            try
            {
                Console.WriteLine($"演示 使用 Microsoft.Extensions.DependencyInjection 进行依赖注入");

                AutoGetText = _textService?.GetText();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"数据初始化出错：{ex}");
            }
        }

        #region 命令

        /// <summary>
        /// [命令] 手动获取文字
        /// </summary>
        public ICommand ManualGetTextCmd { get; private set; }

        #endregion

        /// <summary>
        /// 命令方法赋值(在构造函数中调用)
        /// </summary>
        private void SetCommandMethod()
        {
            ManualGetTextCmd = new RelayCommand(obj => true, obj =>
            {
                //方式二：主动从容器中获取；
                ITextService textService = App.ServiceProvider.GetRequiredService<ITextService>();
                if (textService == null)
                {
                    Console.WriteLine($"从容器中获取文字服务失败");
                }
                else
                {
                    ManualGetText = textService.GetText();
                    //var win = App.ServiceProvider.GetService<WinDITester>();
                    //ToastToWindowCmd.Execute(new List<object>() { $"获取成功", win });
                    ToastToScreenCmd.Execute($"获取成功");
                }
            });
        }
    }
}
